<?php
 //设置页面显示的文字编码
   header("Content-Type:text/html;charset=utf-8");
$servername = "localhost";
$username = "ysl_simk_cn";
$password = "XkBzSbXGtDSKbwWr";
$dbname = "myDB";
 
// 定义接口返回值
$success = '';
$error_code = '';
$datalist = array();
$do = trim($_REQUEST['do']);

// 创建连接
$conn = new mysqli($servername, $username, $password);
// $conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
echo "连接成功";



// 创建数据库

public function createDb(){
   $sql = "CREATE DATABASE ysl_simk_cn";
    if ($conn->query($sql) === TRUE) {
        echo "数据库创建成功";
    } else {
        echo "Error creating database: " . $conn->error;
    }
    
    $conn->close();
}
public function createTableUser(){
   // 使用 sql 创建数据表
$sql = "CREATE TABLE User (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
name VARCHAR(30) NOT NULL,
pwd VARCHAR(30) NOT NULL,
)";
 
if ($conn->query($sql) === TRUE) {
    echo "Table MyGuests created successfully";
} else {
    echo "创建数据表错误: " . $conn->error;
}
 
$conn->close();
}

public function createUser($name,$pwd){ 
    
    $sql = "INSERT INTO User (name, pwd) VALUES ($name,$pwd)";
    
    if ($conn->query($sql) === TRUE) {
      // 已收藏,请勿重复添加
        $success = '1';      
    } else {
       $error_code = '-1';  // 
    }
    // 返回json数据，或者字符串，数字。
    $json = array('success' = >$success, 'error_code' = >$error_code, 'datalist' = >$datalist);
    echo json_encode($json);
    $conn->close();
}
public function updateUser($id,$name)
{
   $sql="UPDATE User SET name=$name WHERE id=$id");        
    if ($conn->query($sql) === TRUE) {
      // 已收藏,请勿重复添加
        $success = '1';      
    } else {
       $error_code = '-1';  // 
    }
    // 返回json数据，或者字符串，数字。
    $json = array('success' = >$success, 'error_code' = >$error_code, 'datalist' = >$datalist);
    echo json_encode($json);
    $conn->close();
}
public function deleteUser($id){
    if ($conn->query($sql) === TRUE) {
      // 已收藏,请勿重复添加
        $success = '1';      
    } else {
       $error_code = '-1';  // 
    }
    // 返回json数据，或者字符串，数字。
    $json = array('success' = >$success, 'error_code' = >$error_code, 'datalist' = >$datalist);
    echo json_encode($json);
    $conn->close();
}
public function getUser($id ){
    $sql = "SELECT * FROM User WHERE id=$id ";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        // 输出数据
        while($row = $result->fetch_assoc()) {
        $datalist[]=$row;
        }
    } else {
        echo "0 结果";
    }
    $conn->close();
    $json = array('success' = >$success, 'error_code' = >$error_code, 'datalist' = >$datalist);
    echo json_encode($json);
    $conn->close();
}
public function getUers(){
    $sql = "SELECT * FROM User";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        // 输出数据
        while($row = $result->mysql_fetch_array()) {
            $datalist[]=$row;
        }
    } else {
        echo "0 结果";
    }    
    $json = array('success' = >$success, 'error_code' = >$error_code, 'datalist' = >$datalist);
    echo json_encode($json);
    $conn->close();
}
 
// http://www.cnblogs.com/xiaogou/p/9252757.html
//  require_once('conn.php');
//     /*
//      *选择数据表 
//      * */
//     $sqla = "SELECT * from user";
//     $result = mysql_query($sqla,$conn);
//     $dataarr = array();
//     while($row = mysql_fetch_array($result)){
//         $dataarr[]=$row;
//     }
//     $id=$_GET['id'];
//     if($id==1){
//         Response::json(1,'数据返回成功',$dataarr);
//     }else if($id==2){
//         Message::json(0,'失败');
//     }


?>